import { getToken, setToken, removeToken } from "../config/auth"

const state = {
  token: getToken(),
  userInfo:{}
}
const mutations = {
  setToken(state,token){
    state.token = token 
    setToken(token)  // 需要同步更新到缓存当中
  },
  removeToken(state){
    state.token = null
    removeToken()  // 需要删掉缓存里面的token
  },
  setUserInfo(state,userInfo){
    state.userInfo = userInfo
  }
}
const actions = {
  login(context,data){
    // 登录的时候调用actions 将token设给newtoken
    context.commit('setToken',data) 
  },
  loginOut(context){
    context.commit('removeToken')
  },
  handleUserInfo(context,data){
    context.commit('setUserInfo',data)
  }
}


export default {
  namespaced:true,
  state,
  mutations,
  actions
}